package com.northpool.operator.common.crypto;

import cn.hutool.crypto.SecureUtil;
import com.northpool.operator.annotation.InputParam;
import com.northpool.operator.annotation.InputParams;
import com.northpool.operator.annotation.OperatorInfo;
import com.northpool.operator.annotation.OutputParam;
import com.northpool.operator.annotation.OutputParams;
import com.northpool.operator.common.algorithm.OperatorTestBuild;
import com.northpool.operator.common.algorithm.TestParamBuilder;
import com.northpool.operator.param.ParamTool;
import com.northpool.operator.type.TypeEnum;
import com.northpool.operator.utils.StpOperatorHelper;



@OperatorInfo(
        ref = "EncryptOperator",
        name = "加密",
        version = "md5",
        description = "字符串加解密算子"
)
@OutputParams({
        @OutputParam(ref = "md5", name = "md5值", desc = "字符串的md5值")
})
public class Md5EncryptOperator extends StpOperatorHelper {

    @InputParams({
            @InputParam(ref = "str", name = "待加密字符串", desc = "待Md5加密字符串", type = TypeEnum.STRING, required = true),
            @InputParam(ref = "salt", name = "盐值", desc = "盐值将放到字符串前面", type = TypeEnum.STRING, required = false, value = "")
    })
    public Md5EncryptOperator() {
    }

    public static void main(String[] args) {
        OperatorTestBuild.execute(Md5EncryptOperator.class, TestParamBuilder.aBuilder()
                .param("str", "123456")
                .build());
    }

    @Override
    public void execute(ParamTool paramTool) throws Exception {
        String str = paramTool.getStr("str");
        String salt = paramTool.getStr("salt");

        if (salt != null) {
            str = salt + str;
        }
        String digestHex = SecureUtil.md5(str);
        this.result("md5", digestHex);
    }
}
